package com.xg.te.strategy;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

/**
 * @desc 定时策略
 * @author liuguojun
 * @date 2014-07-16
 */
public class TimeStrategy extends Strategy {

	private static final long MMS = 60000L; // 一分钟的毫秒数
	private static final long HMS = 3600000L; // 一小时的毫秒数
	private static final long DMS = 86400000L; // 一天的毫秒数
	private static final long YMS = 31536000000L; // 一年的毫秒数

	private Map<String, Long> moniter = new HashMap<String, Long>();

	public void execute(JSONObject jsonObj) {
		try {
			// 获取刷新时间
			String strategy = jsonObj.getString("strategy");
			int lastIndex = strategy.length() - 1;
			char c = strategy.charAt(lastIndex);
			long st = YMS;
			if (c == 'm') {
				st = MMS
						* Integer
								.parseInt(strategy.substring(0, lastIndex - 1));
			} else if (c == 'h') {
				st = HMS
						* Integer
								.parseInt(strategy.substring(0, lastIndex - 1));
			} else if (c == 'd') {
				st = DMS
						* Integer
								.parseInt(strategy.substring(0, lastIndex - 1));
			}
			// 获取当时时间
			Long ct = Calendar.getInstance().getTimeInMillis();

			for (Iterator<String> it = pages.iterator(); it.hasNext();) {
				String pageCode = it.next();
				Long mt = moniter.get(pageCode);

				String[] arr = pageCode.split("_");
				if (arr.length != 2) {
					continue;
				}
				if (mt == null || ct > mt + st) {
					staticHtml(arr[0], arr[1]);
					moniter.put(pageCode, ct);
				}
			}
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
		}
	}

}
